module gui.manager;

// Allegro 5 imports
import allegro5.allegro;

// AGui imports
import AGui.Backends.Allegro5.Allegro5;
import AGui.AGui;

public class Gui
{
    private GuiManager _guiMan;

    private Allegro5Input inputHandler;
    private Allegro5Graphics graphicsHandler;
    
    private Font defaultFont;


    this()
    {
        _guiMan = null;

        inputHandler = null;
        graphicsHandler = null;

        defaultFont = null;
    }

    void initializeAgui()
    {
        //Set the image loader
        Image.setImageLoader(new Allegro5ImageLoader);
        
        //Set the font loader
        Font.setFontLoader(new Allegro5FontLoader);
        
        //Instance the input handler
        inputHandler = new Allegro5Input();
        
        //Instance the graphics handler
        graphicsHandler = new Allegro5Graphics();
        
        //Allegro does not automatically premultiply alpha so let Agui do it
        Color.setPremultiplyAlpha(true);
        
        //Instance the gui
        _guiMan = new GuiManager();
        
        //Set the input handler
        _guiMan.setInput(inputHandler);
        
        //Set the graphics handler
        _guiMan.setGraphics(graphicsHandler);
        
        //defaultFont = Font.load("Fonts/DejaVuSans.ttf", 9);
        defaultFont = Font.load("Fonts/arial.ttf", 9);
        
        //Setting a global font is required and failure to do so will crash.
        Widget.setGlobalFont(defaultFont);
    }
    
    void cleanUpAgui()
    {
        _guiMan.getTop().clear();
        
        _guiMan = null;
        inputHandler = null;
        graphicsHandler = null;
        defaultFont = null;
    }

    void processEvent(ALLEGRO_EVENT event)
    {
        //Let Agui process the event
        inputHandler.processEvent(event);
    }

    @property nothrow public GuiManager getManager() { return _guiMan; }
}

